//
//  MakeDetailTableViewCell.m
//  Food
//
//  Created by dlios on 15-7-18.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "MakeDetailTableViewCell.h"
#import "BaseHeader.h"
#import "UIImageView+WebCache.h"
@implementation MakeDetailTableViewCell

- (void)dealloc
{
    [_model release];
    [_numLabel release];
    [_infoLabel release];
    [_myImageView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.numLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.numLabel];
        [_numLabel release];
        
        self.infoLabel = [[UILabel alloc] init];
        self.infoLabel.font = [UIFont systemFontOfSize:14];
        self.infoLabel.numberOfLines = 0;
        [self.infoLabel sizeToFit];
        [self.contentView addSubview:self.infoLabel];
        [_infoLabel release];
        
        self.myImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.myImageView];
        [_myImageView release];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.numLabel.frame = CGRectMake(10, 5, tableViewWidth / 10, tableViewHeight / 6);
    self.infoLabel.frame = CGRectMake(tableViewWidth / 10, 5, tableViewWidth / 10 * 9, [[self class] heightWithText:self.infoLabel.text]);
    self.myImageView.frame = CGRectMake(tableViewWidth / 10, self.infoLabel.frame.size.height + 5, 140, 100);
}

#warning 2.通过字符串计算高度
+ (CGFloat)heightWithText:(NSString *)text
{
    // 设置一个字典 保存文本属性
    // 保存文本字体大小 和label设置的大小一致
    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};
    // 预设一个尺寸大小 文本最大不超过这个尺寸
    CGSize size = CGSizeMake(375 - 10, 1000);
    // 根据文本内容 获得一个CGRect
    // 参数1: 尺寸范围
    // 参数2: 按照什么方式获取Rect
    // 参数3: 文本属性
    // 参数4: nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    // 返回得到的高度
    return rect.size.height;
}
- (void)setModel:(MakeDetailObject *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    self.numLabel.text = model.num;
    self.infoLabel.text = model.info;
//    self.infoLabel.numberOfLines = 0;
//    [self.infoLabel sizeToFit];
    [self.myImageView setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
